package com.SoapClasses;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URLDecoder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.Data.Comment;
import com.Data.Post;
import com.Data.User;
import com.Managers.LogManagerClass;


public class SoapComments {

    SoapSerializationEnvelope envelope;

    //
    PropertyInfo pi;

    SoapObject Request;

    public String addComment(String[] data) {

        String METHOD_NAME = "newComment";
        String SOAP_ACTION = "http://ShareUp.com/newComment";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "@";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e("SoapComments: ", x.getMessage());
            LogManagerClass.writeLog("SoapComments: "
                        + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String trace = " ";
            for (StackTraceElement line : stack) {
                trace += line.toString();
            }
            Log.e("SoapComments: ", trace);
            LogManagerClass.writeLog("SoapComments: " + trace);
        }
        catch (IOException x) {
            Log.e("SoapComments: ", x.getMessage());
            LogManagerClass.writeLog("SoapComments: "
                        + x.getMessage());
        }
        catch (XmlPullParserException x) {
            Log.e("SoapComments: ", x.getMessage());
            LogManagerClass.writeLog("SoapComments: "
                        + x.getMessage());
        }
        //
        Log.e("responseC", response.toString());
        return response.toString();
    }

    public String deleteComment(int commentid) {

        String METHOD_NAME = "deleteComment";
        String SOAP_ACTION = "http://ShareUp.com/deleteComment";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        Request.addProperty("id", commentid);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (Exception x) {
            response = x.getMessage();
            Log.e("SoapComments: ", x.getMessage());
            LogManagerClass.writeLog("SoapComments: "
                        + x.getMessage());

        }

        return response.toString();
    }

    public Comment[] getComments(int IDPICTURE, int LAST) {

        //
        String METHOD_NAME = "getComments";
        String SOAP_ACTION = "http://ShareUp.com/getComments";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", IDPICTURE);
        Request.addProperty("last", LAST);

        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);

        Object response = null;
        //
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (Exception e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e("SoapComments: ", Trace);
            LogManagerClass.writeLog("SoapComments: " + Trace);
        }

        return getCommentInfo(response);
    }

    private Comment[] getCommentInfo(Object soap) {

        String RES = soap.toString();
        try {
            JSONArray jsonArray = new JSONArray(RES);
            Comment[] coms = new Comment[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                Comment comment = new Comment(null);
                User user = new User();
                Post picture = new Post();
                // COMMENT INFO
                int ID = Integer.parseInt(jo.getString("IDComment"));
                String Message = "";
                try {
                    Message = URLDecoder.decode(jo.getString("Message"), "UTF-8");
                }
                catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // PICTURE INFO
                String Pic = jsonArray.getJSONObject(i).getJSONObject("Picture").toString();
                jo = new JSONObject(Pic);
                String PublicationDate = jo.getString("PublicationDate");
                // USER INFO
                String Owner = jsonArray.getJSONObject(i).getJSONObject("Picture").getJSONObject("User").toString();
                jo = new JSONObject(Owner);
                int OwnerId = Integer.parseInt(jo.getString("ID"));
                String OwnerName = jo.getString("RealName");
                String OwnerProfile = jo.getString("ProfilePicture");
                // Setting
                user.setID(OwnerId);
                user.setRealName(OwnerName);
                user.setProfilePicture(OwnerProfile);
                picture.setUser(user);
                picture.setPublicationDate(PublicationDate);
                comment.setPicture(picture);
                comment.setIDComment(ID);
                comment.setMessage(Message);

                coms[i] = comment;

            }
            return coms;

        }
        catch (JSONException x) {
            Log.e("SoapComments: ", x.getMessage());
            LogManagerClass.writeLog("SoapComments: "
                        + x.getMessage());
            return null;
        }

    }
}
